package com.google.vr.cardboard;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;






public class UiLayerExt
{

    public UiLayerExt(Context context1)
    {
        isSettingsButtonEnabled = true;
        isAlignmentMarkerEnabled = true;
        backButtonRunnable = null;
        transitionViewEnabled = false;
        context = context1;
        initializeViewsWithLayoutId(R.layout.ui_layer);
    }

    public void setPortraitSupportEnabled(boolean flag)
    {
        initializeViewsWithLayoutId(flag ? R.layout.ui_layer_with_portrait_support : R.layout.ui_layer);
    }

    private void initializeViewsWithLayoutId(int i)
    {
        rootLayout = (RelativeLayout)LayoutInflater.from(context).inflate(i, null, false);

        backButton = (ImageButton)rootLayout.findViewById(R.id.ui_back_button);
   
        backButton.setVisibility(computeVisibility(false));
        backButton.setOnClickListener(new _cls3());
        alignmentMarker = rootLayout.findViewById(R.id.ui_alignment_marker);
        alignmentMarker.setVisibility(computeVisibility(false));
    }

  

    private static int computeVisibility(boolean flag)
    {
        return !flag ? 8 : 0;
    }

    public View getView()
    {
        return rootLayout;
    }

    public void attachUiLayerToActivity(final Context activityContext)
    {
        if(activityContext == null || !(activityContext instanceof Activity))
        {
            throw new RuntimeException("A valid activityContext must be provided.");
        } else
        {
            ThreadUtils.runOnUiThread(new _cls4(activityContext));
            return;
        }
    }

    public void setEnabled(final boolean enabled)
    {
        ThreadUtils.runOnUiThread(new _cls5(enabled));
    }

    public void setSettingsButtonEnabled(final boolean enabled)
    {
        isSettingsButtonEnabled = enabled;
      //  ThreadUtils.runOnUiThread(new _cls6(enabled));
    }

    public void setSettingsButtonRunnable(Runnable runnable)
    {
        settingsButtonRunnable = runnable;
    }

    public void setBackButtonListener(final Runnable runnable)
    {
        backButtonRunnable = runnable;
        ThreadUtils.runOnUiThread(new _cls7(runnable));
    }

    public void setAlignmentMarkerEnabled(final boolean enabled)
    {
        isAlignmentMarkerEnabled = enabled;
        ThreadUtils.runOnUiThread(new _cls8(enabled));
    }







    public boolean getSettingsButtonEnabled()
    {
        return isSettingsButtonEnabled;
    }

    public boolean getBackButtonEnabled()
    {
        return backButtonRunnable != null;
    }

    public boolean getAlignmentMarkerEnabled()
    {
        return isAlignmentMarkerEnabled;
    }

    private final Context context;
    private ImageButton settingsButton;
    private ImageButton backButton;
    private View alignmentMarker;
    private TransitionView transitionView;
    private RelativeLayout rootLayout;
    private volatile boolean isSettingsButtonEnabled;
    private volatile boolean isAlignmentMarkerEnabled;
    private volatile Runnable backButtonRunnable;
    private volatile Runnable settingsButtonRunnable;
    private volatile boolean transitionViewEnabled;
    private volatile String viewerName;




    private class _cls3
        implements android.view.View.OnClickListener
    {

        public void onClick(View view)
        {
            Runnable runnable;
            if((runnable = backButtonRunnable) != null)
                runnable.run();
        }

        final UiLayerExt this$0;

        _cls3()
        {
        	super();
            this$0 = UiLayerExt.this;
           
        }
    }


    private class _cls4
        implements Runnable
    {

        public void run()
        {
            ((Activity)activityContext).addContentView(rootLayout, new android.widget.RelativeLayout.LayoutParams(-1, -1));
        }

        final Context activityContext;
        final UiLayerExt this$0;

        _cls4(Context context)
        {
        	super();
            this$0 = UiLayerExt.this;
            activityContext = context;
          
        }
    }


    private class _cls5
        implements Runnable
    {

        public void run()
        {
            rootLayout.setVisibility(UiLayerExt.computeVisibility(enabled));
        }

        final boolean enabled;
        final UiLayerExt this$0;

        _cls5(boolean flag)
        {
        	super();
        	
            this$0 = UiLayerExt.this;
            enabled = flag;
            
        }
    }


    private class _cls6
        implements Runnable
    {

        public void run()
        {
            settingsButton.setVisibility(UiLayerExt.computeVisibility(enabled));
        }

        final boolean enabled;
        final UiLayerExt this$0;

        _cls6(boolean flag)
        {
        	super();
        	
            this$0 = UiLayerExt.this;
            enabled = flag;
            
        }
    }


    private class _cls7
        implements Runnable
    {

        public void run()
        {
            boolean flag = runnable != null;
            backButton.setVisibility(UiLayerExt.computeVisibility(flag));
            if(transitionView != null)
                transitionView.setBackButtonListener(runnable);
        }

        final Runnable runnable;
        final UiLayerExt this$0;

        _cls7(Runnable runnable1)
        {
        	super();
        
            this$0 = UiLayerExt.this;
            runnable = runnable1;
            
        }
    }


    private class _cls8
        implements Runnable
    {

        public void run()
        {
            alignmentMarker.setVisibility(UiLayerExt.computeVisibility(enabled));
        }

        final boolean enabled;
        final UiLayerExt this$0;

        _cls8(boolean flag)
        {
        	super();
        	
            this$0 = UiLayerExt.this;
            enabled = flag;
            
        }
    }







}